/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.co.pas.business.data;

import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

/**
 *
 * @author alabuschagne
 */
@Entity
public class Province implements Serializable
{
    private String code = "";
    private String name = "";
    private static final long serialVersionUID = 1L;
    private Country country;
    private boolean processed = false;
    public Province()
    {
    }

    public Province(Country country, String code, String name)
    {
        setCountry(country);
        setCode(code);
        setName(name);
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (code!= null ? code.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Province))
        {
            return false;
        }
        Province other = (Province) object;
        if ((this.code == null && other.code != null) || (this.code != null && (this.code.compareTo(other.code) !=0)))
        {
            return false;
        }
        return true;
    }   

    /**
     * @return the country
     */
    @ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
    public Country getCountry()
    {
        return country;
    }

    /**
     * @param country the country to set
     */
    public void setCountry(Country country)
    {
        this.country = country;
    }

    @Id
    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    /**
     * @return the name
     */
    public String getName()
    {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name)
    {
        this.name = name;
    }

    public boolean check(Province otherProvince)
    {
        if(otherProvince == null)
        {
            return false;
        }
        return ((code.compareTo(otherProvince.getCode()) == 0) && (name.compareTo(otherProvince.getName()) == 0));
    }

    @Override
    public Province clone()
    {
        Province newProvince = new Province();
        newProvince.setCode(code);
        newProvince.setName(name);
        return newProvince;
    }

    

    /**
     * @return the processed
     */
    @Transient
    public boolean isProcessed()
    {
        return processed;
    }

    /**
     * @param processed the processed to set
     */
    public void setProcessed(boolean processed)
    {
        this.processed = processed;
    }

    @Override
    public String toString()
    {
        return getName();
    }

    public String logPrint(int level)
    {
        StringBuilder tab = new StringBuilder();
        for(int i = 0; i < level; i++)
        {
            tab.append("\t");
        }
        StringBuilder sb = new StringBuilder(tab.toString());
        sb.append("PROVINCE\n");
        sb.append(tab.toString());
        sb.append("Code: \t");
        sb.append(code);
        sb.append("\n");
        sb.append(tab.toString());
        sb.append("Name: \t");
        sb.append(name);
        return sb.toString();
    }
}
